package com.zbkj.crmeb.merchant.admin.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.zbkj.crmeb.agent.service.AgentService;
import com.zbkj.crmeb.marketing.model.StoreCoupon;
import com.zbkj.crmeb.marketing.request.StoreCouponSearchRequest;
import com.zbkj.crmeb.merchant.admin.service.MerAgentStoreOrderService;
import com.zbkj.crmeb.store.model.OrderInfoDelivery;
import com.zbkj.crmeb.store.model.StoreOrderStatus;
import com.zbkj.crmeb.store.request.DeliveryRequest;
import com.zbkj.crmeb.store.request.StoreOrderSearchRequest;
import com.zbkj.crmeb.store.request.StoreOrderStatusSearchRequest;
import com.zbkj.crmeb.store.response.DeliveryResponse;
import com.zbkj.crmeb.store.response.StoreOrderAgentResponse;
import com.zbkj.crmeb.store.response.StoreOrderInfoResponse;
import com.zbkj.crmeb.store.service.OrderInfoDeliveryService;
import com.zbkj.crmeb.store.service.StoreOrderStatusService;
import com.zbkj.crmeb.store.service.SupplierService;
import com.zbkj.crmeb.system.service.SystemCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 服务商-订单管理
 *
 * @author lvmd
 * @date: 2022-02-22
 */
@RestController
@RequestMapping("api/admin/mer/agent/store/order")
@Api(tags = "服务商-订单管理")
public class MerAgentStoreOrderController extends AdminBaseController {

    @Resource
    private MerAgentStoreOrderService agentStoreOrderService;
    @Resource
    private AgentService agentService;
    @Resource
    private SupplierService supplierService;
    @Resource
    private SystemCityService systemCityService;
    @Resource
    private StoreOrderStatusService storeOrderStatusService;
    @Resource
    private OrderInfoDeliveryService orderInfoDeliveryService;

    /**
     * 分页显示订单表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    public CommonResult<StoreOrderAgentResponse> listStoreOrders(@Validated StoreOrderSearchRequest request,
                                                                 @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(agentStoreOrderService.listStoreOrders(request, pageParamRequest));
    }

    /**
     * 查询订单表信息
     *
     * @param id Integer
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    public CommonResult<StoreOrderInfoResponse> info(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(agentStoreOrderService.info(id));
    }

//    @ApiOperation(value = "服务商获取所有供应商")
//    @GetMapping(value = "/getAllSupplier")
//    public CommonResult<Object> getList() {
//        Agent agent = Optional.ofNullable(agentService.getById(systemAdminService.getAdminAgentId())).orElse(new Agent());
//        JSONArray jsonArray = JSONArray.parseArray(agent.getSuppliersIds());
//        return CommonResult.success(supplierService.list(Wrappers.<Supplier>lambdaQuery()
//                .in(!jsonArray.isEmpty(), Supplier :: getId, jsonArray.toJavaList(Integer.class) )
//                .eq(Supplier::getIsDeleted, false)
//                .isNull(Supplier::getMerId)
//        ));
//    }

    /**
     * 城市服务树形结构数据
     */
    @ApiOperation(value = "树形结构")
    @GetMapping(value = "/city/list")
    public CommonResult<Object> register() {
        return CommonResult.success(systemCityService.getListTree());
    }

    /**
     * 分页显示订单操作记录表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "分页列表") //配合swagger使用
    @GetMapping(value = "/status/list")
    public CommonResult<CommonPage<StoreOrderStatus>> getList(
            @Validated StoreOrderStatusSearchRequest request,
            @Validated PageParamRequest pageParamRequest) {
        request.setOperating(3);
        CommonPage<StoreOrderStatus> storeOrderStatusCommonPage = CommonPage.restPage(storeOrderStatusService.getList(request, pageParamRequest));
        return CommonResult.success(storeOrderStatusCommonPage);
    }

    @ApiOperation(value = "优惠券下拉列表")
    @GetMapping(value = "/couponList")
    public CommonResult<List<StoreCoupon>> listCoupons(StoreCouponSearchRequest storeCouponSearchRequest) {
        List<StoreCoupon> storeCouponCommons = agentStoreOrderService.listCoupons(storeCouponSearchRequest);
        return CommonResult.success(storeCouponCommons);
    }

    @ApiOperation(value = "查询物流信息")
    @GetMapping(value = "/queryShowApi")
    public CommonResult<DeliveryResponse> queryShowApi(@Validated DeliveryRequest request) {
        if (Objects.isNull(request.getId())) {
            return CommonResult.failed("物流id必传!");
        }
        OrderInfoDelivery orderInfoDelivery = orderInfoDeliveryService.getById(request.getId());
        if (null == orderInfoDelivery) {
            return CommonResult.failed("暂无物流信息!");
        }
        request.setPhone(orderInfoDelivery.getPhone());
        request.setCom(orderInfoDelivery.getDeliveryCode());
        request.setDeliveryNumber(orderInfoDelivery.getDeliveryNumber());
        request.setOrderInfoId(orderInfoDelivery.getOrderInfoId());
        return CommonResult.success(orderInfoDeliveryService.queryDeliveryByShowApi(request, orderInfoDelivery));
    }

}



